/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gota.processingResources;

import gate.*;
import gate.creole.SerialAnalyserController;
import java.io.File;
import org.w3c.dom.Node;
import javax.xml.xpath.*;

/**
 *
 * @author Administrator
 */
public class OpenNLPPOSTagger implements IProcessingResource {

    @Override
    public void init(SerialAnalyserController controller, Node node)
            throws Exception {
        XPath xpath = XPathFactory.newInstance().newXPath();
        org.w3c.dom.Node modelUrlNode = (org.w3c.dom.Node) xpath.evaluate(
                "//ModelFilePath", node, XPathConstants.NODE);
        org.w3c.dom.Node tagDictionaryNode = (org.w3c.dom.Node) xpath
                .evaluate("//TagDictionaryFilePath", node, XPathConstants.NODE);
        org.w3c.dom.Node encodingNode = (org.w3c.dom.Node) xpath
                .evaluate("//Encoding", node, XPathConstants.NODE);
        String encoding = "UTF-8";
        if(encodingNode ==null){
            encoding = encodingNode.getTextContent();
        }
        String modelUrl = modelUrlNode.getTextContent();
        String tagDictionary = tagDictionaryNode.getTextContent();
        FeatureMap params = Factory.newFeatureMap();
        params
                .put(
                "dictionary",
                new File(tagDictionary).toURI().toURL());
        params
                .put(
                "model",
                new File(modelUrl).toURI().toURL());
        FeatureMap features = Factory.newFeatureMap();
        gate.opennlp.OpenNlpPOS openNLPPOS = (gate.opennlp.OpenNlpPOS) Factory
                .createResource("gate.opennlp.OpenNlpPOS",
                params, features);
        controller.add(openNLPPOS);
    }

    @Override
    public String getResourceName() {
        return "OpenNLPPOSTagger";
    }

}
